#第五章 存储数据

from utils.commonUtil import get_bs4_obj
import csv,re

#采集网页表格的数据 保存在 csv 文件

url = "http://en.wikipedia.org/wiki/Comparison_of_text_editors"

bs4Obj = get_bs4_obj(url)

table = bs4Obj.findAll("table",{'class':re.compile('wikitable.*')})[0]
rows = table.findAll("tr")

csvFile = open("../files/editor.csv",'wt',newline="",encoding="utf-8")
writer = csv.writer(csvFile) # 首先读取一个文件句柄作为数据的写入保存

try:
    for row in rows:
        csvRow = []
        for cell in row.findAll(['td','th']):
            csvRow.append(cell.get_text()) # 读取数据
        writer.writerow(csvRow) #写入数据
        csvRow = []
finally:
    csvFile.close()